package requests

import (
	"card/app/requests/validators"
	"github.com/gin-gonic/gin"
	"github.com/thedevsaddam/govalidator"
)

type SignupTelExistRequest struct {
	Phone string `json:"phone,omitempty" valid:"tel"`
}
type TelIsExistsRequest struct {
	Tel string `json:"tel"`
}

func SignupTelExist(data interface{}, c *gin.Context) map[string][]string {

	// 自定义验证规则
	rules := govalidator.MapData{
		"tel": []string{"required", "digits:11"},
	}

	// 自定义验证出错时的提示
	messages := govalidator.MapData{
		"phone": []string{
			"required:手机号为必填项，参数名称 phone",
			"digits:手机号长度必须为 11 位的数字",
		},
	}
	return validate(data, rules, messages)
}

type SignupEmailExistRequest struct {
	Email string `json:"email,omitempty" valid:"email"`
}

func SignupEmailExist(data interface{}, c *gin.Context) map[string][]string {

	// 自定义验证规则
	rules := govalidator.MapData{
		"email": []string{"required", "min:4", "max:30", "email"},
	}
	// 自定义验证出错时的提示
	messages := govalidator.MapData{
		"email": []string{
			"required:Email 为必填项",
			"min:Email 长度需大于 4",
			"max:Email 长度需小于 30",
			"email:Email 格式不正确，请提供有效的邮箱地址",
		},
	}
	return validate(data, rules, messages)
}

type RegisterByEmailRequest struct {
	Email           string `json:"email" valid:"email"`
	Password        string `json:"password" valid:"password"`
	InviteCode      string `json:"invite_code" valid:"invite_code"`
	ConfirmPassword string `json:"confirm_password" valid:"confirm_password"`
	VerifyCode      string `json:"verify_code" valid:"verify_code"`
}

func RegisterByEmail(data interface{}, c *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"email":            []string{"required", "email"},
		"password":         []string{"required", "min:6", "max:16"},
		"confirm_password": []string{"required"},
		"verify_code":      []string{"required", "digits:6"},
	}
	messages := govalidator.MapData{
		"email":            []string{"required:邮箱为必填项", "email:邮箱格式错误"},
		"password":         []string{"required:密码必须填写", "min:密码最短6位", "max:密码最长16位"},
		"confirm_password": []string{"required:确认密码必须填写"},
		"verify_code":      []string{"required:验证码必须填写", "digits:验证码是6位数字"},
	}
	errs := validate(data, rules, messages)
	_data := data.(*RegisterByEmailRequest)
	errs = validators.ValidatePasswordConfirm(_data.Password, _data.ConfirmPassword, errs)
	errs = validators.ValidateVerifyCode(_data.Email, _data.VerifyCode, errs)
	return errs
}
